package com.jimmy.dreamer;

import android.app.Activity;
import android.os.Bundle;
import static android.provider.BaseColumns._ID;
import static com.jimmy.dreamer.DbConstants.USRNAME;
import static com.jimmy.dreamer.DbConstants.PWD;
import static com.jimmy.dreamer.DbConstants.TABLE_NAME;
import static com.jimmy.dreamer.Dreamer.isLogin;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.AlertDialog;

public class Register extends Activity implements OnClickListener{
	private DbHelper dbhelper = null;

    private EditText username = null;
    private EditText password = null;
    private EditText passwordConfirm = null;
    private Button btnDone = null;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		
		initView();

        openDatabase();
	}
	@Override
    protected void onDestroy() {
        super.onDestroy();
        closeDatabase();
    }
    private void openDatabase(){
        dbhelper = new DbHelper(this); 
    }

    private void closeDatabase(){
        dbhelper.close();
    }

    private void initView(){
    	username = (EditText) findViewById(R.id.et_username);
        password = (EditText) findViewById(R.id.et_password);
        passwordConfirm = (EditText) findViewById(R.id.et_passconf);
        btnDone = (Button) findViewById(R.id.btn_regDone);
        btnDone.setOnClickListener(this);
    }
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_regDone:
        	if(checkDuplicate()){
        		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        		alertDialog.setTitle("The username has been used!");
        		alertDialog.setMessage("Please try another username.");
        		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        		   public void onClick(DialogInterface dialog, int which) {
        			   //DO nothing
        		   }
        		});
        		alertDialog.setIcon(R.drawable.ic_launcher);
        		alertDialog.show();
        	}
        	else{
            	if(password.getText().toString().equals(passwordConfirm.getText().toString())){
            		add();
            		isLogin = true;
                	Intent d = new Intent(this, Dreamer.class);
            		startActivity(d);
            	}	
                else{
                	AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            		alertDialog.setTitle("Wrong Password!");
            		alertDialog.setMessage("Please type in the exactly same password!");
            		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            		   public void onClick(DialogInterface dialog, int which) {
            			   //Do nothing
            		   }
            		});
            		alertDialog.setIcon(R.drawable.ic_launcher);
            		alertDialog.show();
                }
        	}
            break;

        default:
        }
    }
    private void add(){
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(USRNAME, username.getText().toString());
        values.put(PWD, password.getText().toString());
        db.insert(TABLE_NAME, null, values);
    }
    private Cursor getCursor(){
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        String[] columns = {_ID, USRNAME, PWD};

        Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
        startManagingCursor(cursor);

        return cursor;
    }
    private boolean checkDuplicate(){
    	Cursor cursor = getCursor();
    	while(cursor.moveToNext()){
    		if(cursor.getString(1).equals(username.getText().toString())) return true;
    	}
    	return false;
    }

}
